package com.example.xunua_zhinengjiaotong.Dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;


public class CarSpeedDialog4 extends AlertDialog{
    private Context context;
    private Spinner spinner;
    private RadioGroup rg;
    private RadioButton rb_start,rb_stop;
    private Button btn_set;
    private MyApp myApp;
    private HttpUtil util=new HttpUtil();

    
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
            }
        }
    };

    public CarSpeedDialog4(Context context, MyApp myApp) {
        super(context,R.style.myDialog);
        this.context=context;
        this.myApp=myApp;
    }

    protected CarSpeedDialog4(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected CarSpeedDialog4(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_car_speed4);
        initView();
    }

    private void initView() {
        rg=findViewById(R.id.car4_rg);
        rb_start=findViewById(R.id.car4_rb_start);
        rb_stop=findViewById(R.id.car4_rb_stop);
        btn_set=findViewById(R.id.car4_btn_set);
       spinner = findViewById(R.id.trolley_dialog4_spinner);
       spinner.setAdapter(new ArrayAdapter<>(context, R.layout.support_simple_spinner_dropdown_item,
               new String[]{"一号小车", "二号小车", "三号小车", "四号小车"}));
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id= (int) (spinner.getSelectedItemId()+1);
                String action=null;
                switch (rg.getCheckedRadioButtonId()){
                    case R.id.car4_rb_start:
                        action="Start";
                        break;
                    case R.id.car4_rb_stop:
                        action="Stop";
                        break;
                }
                if (action .equals(null) ) {
                    Toast.makeText(context, "未选择", Toast.LENGTH_SHORT).show();
                }
                setCar(id+"",action);
            }
        });
    }

    private void setCar(final String id, final String action){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject setCarMove = util.connectServer("{\"CarId\":" + id + ", \"CarAction\":\"" + action + "\"}", myApp, "SetCarMove");
                    String result = setCarMove.getString("result");
                    if ("ok".equals(result)){
                        handler.sendEmptyMessage(1);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
